package com.sugon.modules.excel.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.sugon.dao.FactoryTypeDao;
import com.sugon.dao.SpecialDevCheckRecordDao;
import com.sugon.entity.FactoryTypeEntity;
import com.sugon.entity.SpecialDevCheckRecordEntity;
import com.sugon.entity.SysDeptEntity;
import com.sugon.modules.excel.IExcelHandler;
import com.sugon.service.SpecialDevCheckRecordService;
import com.sugon.service.SysDeptService;
import com.sugon.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 抽象的ExcelHandler，为子类提供公共方法
 *
 * @author YuXD
 * @since 2021-09-18
 */
public abstract class AbstractExcelHandler implements IExcelHandler {

    @Autowired
    private SysDeptService deptService;
    @Autowired
    private FactoryTypeDao factoryTypeDao;
    @Autowired
    private SpecialDevCheckRecordService specialDevCheckRecordService;
    @Autowired
    private SpecialDevCheckRecordDao specialDevCheckRecordDao;

    /**
     * 使用单位Map，用于根据使用单位名称获取使用单位详细信息用
     */
    private Map<String, SysDeptEntity> userDeptMap = new HashMap<>();

    /**
     * 生产厂家Map，用于根据使用单位名称获取使用单位详细信息用
     */
    private Map<String, FactoryTypeEntity> factoryMap = new HashMap<>();

    /**
     * 根据使用单位名称，获取使用单位信息
     *
     * @param deptName
     * @return
     */
    protected SysDeptEntity getDeptByName(String deptName) {
        SysDeptEntity dept = userDeptMap.get(deptName);
        if (dept != null) {
            return dept;
        }
        List<SysDeptEntity> matchedDeptList = deptService.getByName(deptName);
        if (CollectionUtil.isNotEmpty(matchedDeptList)) {
            dept = matchedDeptList.get(0);
            userDeptMap.put(deptName, dept);
        }
        return dept;
    }

    /**
     * 根据生产厂家名称，获取生产厂家详细信息
     *
     * @param factoryName 生产厂家名称
     * @return
     */
    protected FactoryTypeEntity getFactoryByName(String factoryName) {
        FactoryTypeEntity factory = factoryMap.get(factoryName);
        if (factory == null) {
            // 默认获取设备厂家
            List<FactoryTypeEntity> matchedFactoryList = factoryTypeDao.queryObjectByName(factoryName, 0);
            if (CollectionUtil.isNotEmpty(matchedFactoryList)) {
                factory = matchedFactoryList.get(0);
                factoryMap.put(factoryName, factory);
            }
        }
        return factory;
    }

    /**
     * 生成下一次检测记录
     *
     * @param devType           特种设备类别
     * @param devId             设备ID
     * @param certificateNumber 校验报告编号
     * @param latestInspectDate 最近一次检测日期
     * @param inspectUnitId     检验机构ID
     * @return
     */
    boolean generateCheckRecord(String devType, String devId, String certificateNumber, Date latestInspectDate, String inspectUnitId) {
        // 检测报告编号必填，否则不生成记录
        if (StrUtil.isEmpty(certificateNumber) || latestInspectDate == null) {
            return false;
        }
        // 检测报告号唯一
        int existsNum = specialDevCheckRecordDao.countByCertificateNumber(certificateNumber);
        Assert.isTrue(existsNum == 0, "检验报告编号[{}]已存在，请确认！", certificateNumber);
        // 生成检测记录
        SpecialDevCheckRecordEntity checkRecord = new SpecialDevCheckRecordEntity();
        checkRecord.setDevType(devType);
        checkRecord.setDevId(devId);
        checkRecord.setCertificateNumber(certificateNumber);
        checkRecord.setCheckDate(latestInspectDate);
        checkRecord.setRecorder(ShiroUtils.getUserId());
        checkRecord.setInspectUnitId(inspectUnitId);
        return specialDevCheckRecordService.save(checkRecord) > 0;
    }

}
